package com.example.football.service;

import com.example.football.dao.PeopleDao;
import com.example.football.mapper.PeopleMapper;
import com.example.football.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class PeopleServiceImpl implements PeopleService {
    @Resource
    private PeopleDao sd;

    @Override
    public List<People> queryAll() {

        return sd.selectAll();
    }

    @Override
    public void removeByPeopleid(Integer id) {
        try {
            sd.deleteByPeopleid(id);
        } catch (Exception e) {
            throw new RuntimeException("根据ID删除有异常");
        }
    }

    @Override
    public List<People> queryByAccount(String account) {

        return sd.selectByAccount(account);
    }

    @Override
    public People queryById(Integer id) {

        return sd.selectById(id);
    }

    @Override
    public void addPeople(People people) {
        try {
            sd.insertPeople(people);
        } catch (Exception e) {
            throw new RuntimeException("添加有异常");
        }
    }

    @Override
    public void changePeople(People people) {
        try {
            sd.updatePeople(people);
        } catch (Exception e) {
            throw new RuntimeException("修改数据异常");
        }
    }

    @Autowired
    private PeopleMapper userMapper;


    /**
     * 注册
     * @param people
     * @return
     */
    public Result rgister(People people){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            People userByName = userMapper.findUserByAccount(people.getAccount());
            if(userByName!=null){
                //如果查询到 说明存在
                result.setMsg("账号存在");
            }else {
                userMapper.register(people);
                result.setMsg("注册成功");
                result.setSuccess(true);
                result.setDetail(people);
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 登录
     */
    public Result login(People people){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            Integer userId = userMapper.login(people);
            if(userId==null){
                result.setMsg("用户名或密码错误");
            }else {
                result.setMsg("登录成功");
                result.setSuccess(true);
                people.setId(userId);
                People userById = userMapper.findUserById(userId);
                result.setDetail(userById);
            }

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result showPeopleliketeam(People id){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            List<Peopleliketeam> liketeam = userMapper.findpeopleliketeam(id);
            result.setDetail(liketeam);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result showPeoplelikestar(People id){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            List<Peoplelikestar> likestar = userMapper.findpeoplelikestar(id);
            result.setDetail(likestar);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result deletePeopleliketeam(Peopleliketeam peopleliketeam){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            userMapper.deletePeopleliketeam(peopleliketeam);
            result.setMsg("删除成功");
            result.setSuccess(true);
            result.setDetail(peopleliketeam);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result insertPeopleliketeam(Peopleliketeam peopleliketeam){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            userMapper.insertPeopleliketeam(peopleliketeam);
            result.setMsg("插入成功");
            result.setSuccess(true);
            result.setDetail(peopleliketeam);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result deletePeoplelikestar(Peoplelikestar peoplelikestar){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            userMapper.deletePeoplelikestar(peoplelikestar);
            result.setMsg("删除成功");
            result.setSuccess(true);
            result.setDetail(peoplelikestar);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result insertPeoplelikestar(Peoplelikestar peoplelikestar){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            userMapper.insertPeoplelikestar(peoplelikestar);
            result.setMsg("插入成功");
            result.setSuccess(true);
            result.setDetail(peoplelikestar);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result updatepeople(People people){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            userMapper.updatePeople(people);
            result.setMsg("修改成功");
            result.setSuccess(true);
            result.setDetail(people);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
